﻿(() => {
    $(() => {
        const $eventEmitter = $('body > .container');

        let reloadGrid = null;
        $('.main').splitter({
            orientation: 'v',
            panel1Size: '300px',
        });
        const advancedSearch = $('.advanced-search').advancedSearchWithCollapse(null, (e) => {
            reloadGrid = () => {
                dataGrid.refresh({ userData: e });
            };
            reloadGrid();
        }, { id: 'register' });
        advancedSearch.expand();

        const gridOperations = [];
        const dataGrid = $('.exam-grid').examGrid({
            id: 'register-exam-grid',
            key: 'oid',
            getOperations() {
                return gridOperations;
            },
            onLoad(loadOptions) {
                return new Promise((resolve, reject) => {
                    let url = `/api/exam/get-list`;
                    let params = [];
                    const institution = getUrlSearch('institution');
                    if (institution) {
                        params.push(`institution=${institution}`);
                    }
                    const applyDepartment = getUrlSearch('applydepartment');
                    if (applyDepartment) {
                        params.push(`applydepartment=${applyDepartment}`);
                    }
                    const applyDoctor = getUrlSearch('applydoctor');
                    if (applyDoctor) {
                        params.push(`applydoctor=${applyDoctor}`);
                    }
                    const query = params.join('&');
                    $.post({
                        dataType: 'json',
                        contentType: 'application/json;charset=UTF-8',
                        url: `${url}?${query}`,
                        data: JSON.stringify(loadOptions),
                        success: result => {
                            if (result.success) {
                                resolve(result)
                            } else {
                                reject(result.error);
                            }
                        }
                    });
                })
            },
            onRowDblClick(oid) {
                window.open(`/register/detail?oid=${encodeURIComponent(oid)}`, '_blank');
            }
        });
        const tools = $.getExamTools($eventEmitter, advancedSearch, dataGrid);
        //设置列表操作列
        gridOperations.push({
            ...tools.viewReport, options: {
                ...tools.viewReport.options,
                text: '查看报告',
                beginGroup: true
            }
        }, {
            ...tools.viewImage, options: {
                ...tools.viewImage.options,
                text: '查看图像'
            }
        });


        $.get('/api/exam/get-advanced-search-view').then(result => {
            if (result.success) {
                if (result.view) {
                    advancedSearch.updateView(result.view, result.data);
                }
            }
        });
    });
})();
